package auto.crud.service;

import auto.crud.mapper.PersonMapper;
import auto.crud.util.AutoInit;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by watso on 2018/2/14.
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public int autoAdd(Map<String, Object> entityMap) {
        try {
            Map<String, Object> autoMap = AutoInit.initAddColumnValue(entityMap);
            personMapper.autoAdd(autoMap);
            Object id = autoMap.get("id");
            if (id==null) return 0;
            return Integer.parseInt(id.toString());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public int autoUpdate(Map<String, Object> entityMap) {
        try {
            Map<String, Object> autoMap = AutoInit.initAddColumnValue(entityMap);
            personMapper.autoAdd(autoMap);
            Object id = autoMap.get("id");
            if (id==null) return 0;
            return Integer.parseInt(id.toString());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
